<template>
  <!-- 危大工程验收计划 -->
  <base-breadcrumb>
    <searchData :searchData="searchOptions" @search="handleSearch" />

    <div class="page-body">
      <a-card :bordered="false">
        <!--        <title-name title="危大工程验收计划" />-->
        <div class="pub-table-select-count">
          <a-icon type="info-circle" theme="twoTone" />
          <span style="margin-right: 12px">
                    危大工程： <span class="totalNum">{{ totalDetails.one || 0 }}</span> 个 &nbsp;&nbsp;&nbsp;
                    超危大工程： <span class="totalNum">{{ totalDetails.two || 0 }}</span> 个
            </span>
        </div>
        <list-table
          ref="table"
          size="default"
          rowKey="key"
          :columns="columns"
          :request="loadData"
          :isEmptyWidth="true"
          :alert="true"
          show-size-changer
          :isHideFixed="true"
          show-quick-jumper
          showPagination="auto"
        >
        </list-table>
      </a-card>
    </div>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components'
import ThemeChoose from '@/components/themeChoose/themeChoose.vue'
import {
  dangerList, dangerListCount,
  deleteAcceptancePlan,
  getAcceptancePlanCount,
  getAcceptancePlanList
} from '@/api/safetySupervision/acceptancePlan'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import StatusDisplay from '@/components/StatusDisplay'
import dayjs from 'dayjs'
import BSelect from '@/components/BComponents/BSelect'
import { getSelectListByType } from '@/api/safetySupervision/common'
import SelectSubcontractor from '@/components/Select/SelectSubcontractor'

export default {
  name: 'a' + Date.now(), components: {
    ThemeChoose,
    STable,
    StatusDisplay
  },
  data() {
    return {
      searchOptions: [
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchComId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '所属项目',
        //   keyValue: 'branchComId',
        //   valueKey: 'projectId',
        //   type: SelectProject
        // },
        {
          name: '计划月份',
          value: 'planMonth',
          type: 'month'
        },
        {
          name: '验收负责人',
          value: 'acceptanceResEmployeeName',
          type: 'input'
        },
        {
          name: '等级',
          valueKey: 'levelTypeId',
          type: BSelect,
          request: async e => {
            const [err, res] = await getSelectListByType({ type: 29 })
            if (!err) {
              const list = res.data || []
              return { code: 200, data: list.filter(e => e.name !== '一般工程').filter((item) => item.enabled) }
            }
            return { code: 500 }
          }
        },
        {
          name: '分包单位名称',
          value: 'subContractorName',
          type: 'input'
        }
        // {
        //   name: '分包单位名称',
        //   keyValue: 'projectId',
        //   valueKey: 'subContractorId',
        //   type: SelectSubcontractor
        // }
      ],
      columns: [
        { title: '计划月份', dataIndex: 'planMonth', },
        { title: '所属分公司', dataIndex: 'branchComName',},
        { title: '所属项目', dataIndex: 'projectName',},
        { title: '验收分类', dataIndex: 'acceptanceTypeName', },
        { title: '部位', dataIndex: 'acceptancePart',},
        { title: '等级', dataIndex: 'levelTypeName', },
        { title: '验收负责人', dataIndex: 'acceptanceResEmployeeName', },
        { title: '计划验收时间', dataIndex: 'acceptanceDate',},
        { title: '分包单位名称', dataIndex: 'subContractorName'},
        { title: '备注', dataIndex: 'remark', },
        { title: '填报人', dataIndex: 'creatorName', },
        { title: '填报时间', dataIndex: 'createTime', },
        { title: '是否验收', dataIndex: 'haveEdit',},
      ],
      queryParam: {},
      totalDetails: {
        one: 0,
        two: 0
      },
      loadData: async (parameter) => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        this.getAcceptancePlanCount(requestParameters)
        const [err, res] = await dangerList(requestParameters)
        return new Promise((resolve, reject) => {
          if (err) {
            reject(err)
          } else {
            resolve(res)
          }
        })
      }
    }
  },
  methods: {
    handleSearch(val = {}) {
      this.queryParam = {
        ...val,
        beginDate:
          (val.beginPlanMonth && val.beginPlanMonth.length && val.beginPlanMonth[0].format('YYYY-MM-DD')) ||
          void 0,
        endDate:
          (val.beginPlanMonth && val.beginPlanMonth.length && val.beginPlanMonth[1].format('YYYY-MM-DD')) ||
          void 0
      }
      delete this.queryParam.beginPlanMonth
      this.$refs.table.refresh(true)
    },
    // 统计
    async getAcceptancePlanCount(param) {
      const [err, res] = await dangerListCount(param)
      if (!err) {
        this.totalDetails = res.data
      }
    },
    async deleteAcceptancePlan(data) {
      const [err, res] = await deleteAcceptancePlan({ id: data.id })
      if (!err) {
        this.$message.success('成功')
        this.$refs.table.refresh(true)
      }
    },
    // 处理更新操作（新增、编辑）
    async handleUpdate(type, data = {}) {
      if (type == 'del') {
        let that = this
        this.$confirm({
          title: '温馨提示',
          content: '确定是否删除数据？',
          okText: '确认',
          okType: 'danger',
          cancelText: '取消',
          onOk: () => {
            return that.deleteAcceptancePlan(data)
          }
        })
      } else {
        this.$router.push({
          name: 'UpdateAcceptancePlan',
          query: {
            id: data.id || '',
            pageType: type
          }
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.page-header {
  .header-row {
    display: flex;

    .flex-content {
      flex: auto;
    }

    .divider {
      margin: 0 12px;
    }

    .expand-btn {
      margin-left: 8px;
      font-size: 14px;
    }
  }
}

.page-body {
  .table-operateors {
    .ant-btn + .ant-btn {
      margin-left: 12px;
    }
  }
}
</style>